package com.baitiaojun.video.handler.canal;

import com.baitiaojun.api.domain.user.UserInfo;
import com.baitiaojun.api.domain.video.Video;
import com.baitiaojun.api.util.AESUtil;
import com.baitiaojun.video.feign.ElasticsearchVideoFeign;
import com.baitiaojun.video.feign.UserFeign;
import com.baitiaojun.video.service.VideoParentCommentService;
import com.baitiaojun.video.service.VideoLikeService;
import com.baitiaojun.video.service.VideoService;
import com.baitiaojun.video.service.VideoViewService;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @类名 AbstractSyncEsHandler
 * @作者 白条君
 * @创建日期 2023/1/25 13:15
 * @版本 1.0
 */
@Component
public class SyncEsHandler {

    @Autowired
    protected VideoLikeService videoLikeService;

    @Autowired
    private VideoService videoService;

    @Lazy
    @Autowired
    protected VideoParentCommentService videoParentCommentService;

    @Autowired
    protected ElasticsearchVideoFeign elasticsearchVideoFeign;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private VideoViewService videoViewService;

    protected void updateVideo(Long videoId) throws Exception {
        Video video = videoService.getVideoById(videoId);
        Integer commentCount = videoParentCommentService.getVideoCommentCount(videoId);
        Integer likeCount = videoLikeService.getVideoLikeCount(videoId);
        Integer viewCount = videoViewService.getVideoViewCount(videoId);
        video.setCommentCount(commentCount);
        video.setLickCount(likeCount);
        video.setViewCount(viewCount);
        //获取用户的昵称
        List<UserInfo> userInfos = userFeign.batchGetUserInfoByUserIdsSet(Sets.newHashSet(video.getUserId()));
        video.setCreator(userInfos.get(0).getNick());
        AESUtil aesUtil = AESUtil.lazyGetAesUtil();
        video.setThumbnail(aesUtil.encrypt(video.getThumbnail()));
        elasticsearchVideoFeign.saveVideo(video);
    }
}
